package com.souku.finance.application.ui.mine;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.SignRecordBean;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.utils.DateUtil;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ShareUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.widget.MonthDateView;
import com.souku.finance.application.widget.WeekDayView;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.Success;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Android on 2016/12/25.
 */

public class CheckinActivity extends BaseActivity implements View.OnClickListener {

    protected WeekDayView weekDateView;
    protected MonthDateView monthDateView;
    protected TextView tvDate;
    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected Button btnCheckin;
    protected TextView tvTimes;
    private SignRecordBean signRecordBean;
    private List<Integer> things = new ArrayList<>();
    private List<Integer> list = new ArrayList<>();

    @Override
    public void internet() {
        getSignrecord();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getSignrecord();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_checkin);
        initView();
        topTitle.setText("签到中心");
    }


    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            monthDateView.go(DateUtil.getDate(signRecordBean.getCurrentTime()));
            things = monthDateView.setDaysHasThingList(list);
            monthDateView.setTextView(tvDate, null);
            monthDateView.setDateClick(new MonthDateView.DateClick() {
                @Override
                public void onClickOnDate() {
                    if (things.contains(monthDateView.getmSelDay()) && !list.contains(monthDateView.getmSelDay())) {
                        for (int i = 0; i < things.size(); i++) {
                            if (things.get(i) == monthDateView.getmSelDay()) {
                                if (i == 0) {
                                    ToastUtil.show("本月连续签到7天可获得额外奖励10积分");
                                } else if (i == 1) {
                                    ToastUtil.show("本月连续签到15天可获得额外奖励25积分");
                                } else if (i == 2) {
                                    ToastUtil.show("本月满签可获得额外奖励55积分");
                                }
                            }
                        }
                    }
                }
            });
            return true;
        }
    });

    private void initView() {
        weekDateView = (WeekDayView) findViewById(R.id.weekDateView);
        monthDateView = (MonthDateView) findViewById(R.id.monthDateView);
        tvDate = (TextView) findViewById(R.id.tv_date);
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(CheckinActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        btnCheckin = (Button) findViewById(R.id.btn_checkin);
        btnCheckin.setOnClickListener(CheckinActivity.this);
        tvTimes = (TextView) findViewById(R.id.tv_times);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            finish();
        } else if (view.getId() == R.id.btn_checkin) {
            sign();
        }
    }

    private void getSignrecord() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("userId", ShareUtil.getInstance().getString(Constant.UserId));
        RequestManager.getInstance().post(UrlConstact.SING_RECORD, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    signRecordBean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), SignRecordBean.class);
                    tvTimes.setText(signRecordBean.getSignCount());
                    if (signRecordBean.isIsSign()) {
//                        btnCheckin.setEnabled(false);
                        btnCheckin.setText("已签到");
                        btnCheckin.setBackgroundResource(R.drawable.solid_red_round);
                    }
                    list = new ArrayList<>();
                    for (String s : signRecordBean.getSignRecord()) {
                        list.add(Integer.parseInt(s.substring(s.length() - 2, s.length())));
                    }
                    handler.sendEmptyMessage(0);
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }


    private void sign() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("userId", ShareUtil.getInstance().getString(Constant.UserId));
        RequestManager.getInstance().post(UrlConstact.RECEIVE_SIGN, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    try {
                        JSONObject object = new JSONObject(response);
                        int success = object.getInt("data");
                        if (success == 0) {
                            ToastUtil.show("已签到");
                        }else {

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    getSignrecord();
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }
}
